package com.cjm.one.common.utils;

import com.cjm.one.model.order.dto.TimeRange;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

public class TimeRangeUtils {
    
    // 解析时间范围参数
    public static TimeRange parseTimeRange(String timeRange) {
        LocalDate today = LocalDate.now();
        return switch (timeRange.toLowerCase()) {
            case "week" -> new TimeRange(
                today.minusWeeks(1).atStartOfDay(),
                today.atTime(23, 59, 59)
            );
            case "month" -> new TimeRange(
                today.minusMonths(1).atStartOfDay(),
                today.atTime(23, 59, 59)
            );
            case "year" -> new TimeRange(
                today.minusYears(1).atStartOfDay(),
                today.atTime(23, 59, 59)
            );
            default -> new TimeRange( // 默认处理30天
                today.minusDays(30).atStartOfDay(),
                today.atTime(23, 59, 59)
            );
        };
    }

    // 获取对比时间范围（示例：当前周 vs 上周）
    public static TimeRange getCompareRange(String timeRange) {
        LocalDate today = LocalDate.now();
        return switch (timeRange.toLowerCase()) {
            case "week" -> new TimeRange(
                today.minusWeeks(2).atStartOfDay(),
                today.minusWeeks(1).atTime(23, 59, 59)
            );
            case "month" -> new TimeRange(
                today.minusMonths(2).atStartOfDay(),
                today.minusMonths(1).atTime(23, 59, 59)
            );
            case "year" -> new TimeRange(
                today.minusYears(2).atStartOfDay(),
                today.minusYears(1).atTime(23, 59, 59)
            );
            default -> new TimeRange( // 30天对比前30天
                today.minusDays(60).atStartOfDay(),
                today.minusDays(30).atTime(23, 59, 59)
            );
        };
    }

    public static List<LocalDateTime> generateDateSequence(TimeRange range) {
        return range.getStartTime().toLocalDate()
                .datesUntil(range.getEndTime().toLocalDate().plusDays(1))
                .map(LocalDate::atStartOfDay)
                .collect(Collectors.toList());
    }
}
